﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using DoVuiKinhThanh.Domain;

namespace DoVuiKinhThanh
{
    using System;
    using System.Configuration;
    using System.Windows.Threading;

    using DoVuiKinhThanh.Helpers;

    /// <summary>
    /// Interaction logic for SlideShow.xaml
    /// </summary>
    public partial class SlideShow : Window
    {
        #region IFields

        private List<TextBox> m_txtAnswers = new List<TextBox>();
        private MediaPlayer m_mediaPlayer = new MediaPlayer();
        private bool m_isWorkingOnSecondaryScreen;

        #endregion

        #region IConstructors

        public SlideShow(ObservableCollection<Quiz> playingQuizes)
        {
            InitializeComponent();
            PlayingQuizes = playingQuizes ?? new ObservableCollection<Quiz>();
            CurrentQuiz = PlayingQuizes.Count > 0 ? PlayingQuizes[0] : new Quiz();
            m_txtAnswers = new List<TextBox>
            {
                m_txtAnswer1,
                m_txtAnswer2,
                m_txtAnswer3,
                m_txtAnswer4
            };
        }

        #endregion

        #region IMethods

        private void Init()
        {
            m_vbCountDownContainer.Visibility = Visibility.Hidden;
            foreach (var textBox in m_txtAnswers)
            {
                textBox.Visibility = Visibility.Visible;
            }
            BindData();
            UpdateTextBoxes(true);
        }

        private void BindData()
        {
            m_grdQuizContainer.DataContext = CurrentQuiz;
            m_grdAnswerContainer.DataContext = CurrentQuiz;
        }

        public void StartShow(bool onSecondaryScreen)
        {
            m_isWorkingOnSecondaryScreen = onSecondaryScreen;
            Show();
        }

        private void OnSlideShowLoaded(object sender, RoutedEventArgs e)
        {
            if (m_isWorkingOnSecondaryScreen)
            {
                WindowState = WindowState.Maximized;
            }
            Init();
        }

        private void OnBtnPreviousQuizClicked(object sender, RoutedEventArgs e)
        {
            var previousQuizNumber = CurrentQuizNumber - 1;
            previousQuizNumber = previousQuizNumber < 0 ? 0 : previousQuizNumber;
            if (PlayingQuizes != null && PlayingQuizes.Count > previousQuizNumber && previousQuizNumber >= 0)
            {
                Quiz previousQuiz = PlayingQuizes.ElementAt(previousQuizNumber);
                CurrentQuiz = previousQuiz ?? CurrentQuiz;
                Init();
            }
        }

        private void OnBtnNextQuizClicked(object sender, RoutedEventArgs e)
        {
            var lastQuizNumber = PlayingQuizes.Count - 1;
            var nextQuizNumber = CurrentQuizNumber + 1;
            nextQuizNumber = nextQuizNumber > lastQuizNumber ? lastQuizNumber : nextQuizNumber;
            if (PlayingQuizes != null && PlayingQuizes.Count > nextQuizNumber && nextQuizNumber > 0)
            {
                Quiz nextQuiz = PlayingQuizes.ElementAt(nextQuizNumber);
                CurrentQuiz = nextQuiz ?? CurrentQuiz;
                Init();
            }
        }

        #endregion

        #region IProperties

        private ObservableCollection<Quiz> PlayingQuizes { get; set; }
        public Quiz CurrentQuiz { get; set; }
        private int CurrentQuizNumber
        {
            get
            {
                var currentPos = -1;
                if (PlayingQuizes != null && CurrentQuiz != null)
                {
                    currentPos = PlayingQuizes.IndexOf(CurrentQuiz);
                }
                return currentPos;
            }
        }
        public string CurrentQuizNumberText
        {
            get
            {
                if (CurrentQuizNumber > 0)
                {
                    return string.Format("Câu đố số {0}", CurrentQuizNumber + 1);
                }
                return string.Empty;
            }
        }

        #endregion

        private void OnBtnBeginClicked(object sender, RoutedEventArgs e)
        {
            m_vbCountDownContainer.Visibility = Visibility.Visible;
            var countDownTimeParam = ConfigurationManager.AppSettings[Constants.APP_SETTING_COUNT_DOWN_TIME];
            int countDownTime;
            if (!int.TryParse(countDownTimeParam, out countDownTime))
            {
                countDownTime = 10;
            }
            ControlHelper.StartCountdown(this, m_txtCountDown, countDownTime, TimeUpHandler);
        }

        private void TimeUpHandler(object sender, EventArgs eventArgs)
        {
            m_mediaPlayer.Open(new Uri(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"Sounds\time_up.mp3")));
            m_mediaPlayer.Play();
        }


        private void OnBtnShowAnswerClicked(object sender, RoutedEventArgs e) {
            m_vbCountDownContainer.Visibility = Visibility.Hidden;
            UpdateTextBoxes(false);
        }

        private void UpdateTextBoxes(bool clear) {
            foreach (var textBox in m_txtAnswers) {
                var answer = textBox.DataContext as Answer;
                if (answer == null) {
                    continue;
                }
                textBox.Foreground = (clear || !answer.IsCorrectAnswer) ? new SolidColorBrush(Color.FromRgb(0 , 0 , 0)) : new SolidColorBrush(Color.FromRgb(0 , 0 , 255));
            }
        }
    }
}